﻿using GenSpringCloudProject.projectconfig;
using GenSpringCloudProject.subproject.config_server;
using GenSpringCloudProject.subproject.eureka_server;
using GenSpringCloudProject.subproject.module;
using MyTech.CodeGenerator.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GenSpringCloudProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void btnGenCode_Click(object sender, EventArgs e)
        {
            ProjectConfig projectConfig = new ProjectConfig();
            projectConfig.projectName = this.txtProject.Text.Trim();
            projectConfig.groupId = this.txtGroupId.Text.Trim();
            projectConfig.artifactId = this.txtArtifactId.Text.Trim();
            projectConfig.version = this.txtVersion.Text.Trim();
            projectConfig.packaging = this.txtPackaging.Text.Trim();

            string basePath = @"E:\code\" + projectConfig.projectName + "//";

            GenProject(projectConfig, basePath);
        }

        public bool GenProject(ProjectConfig projectConfig, string basePath)
        {
            bool result = true;
            FileUtil.SaveCode(basePath + "/pom.xml", GenPom(projectConfig, basePath).ToString());

            EurekaServer.Gen(projectConfig, basePath);
            ConfigServer.Gen(projectConfig, basePath);
            Module.Gen(projectConfig, basePath);

            MessageBox.Show("生成完成.路径为："+ basePath);
            return result;
        }

        public static StringBuilder GenPom(ProjectConfig projectConfig, string basePath)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            sb.AppendLine("<project xmlns=\"http://maven.apache.org/POM/4.0.0\"");
            sb.AppendLine("         xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
            sb.AppendLine("         xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">");
            sb.AppendLine("    <modelVersion>4.0.0</modelVersion>");
            sb.AppendLine("");
            sb.AppendLine("    <groupId>"+ projectConfig.groupId + "</groupId>");
            sb.AppendLine("    <artifactId>"+ projectConfig.projectName + "</artifactId>");
            sb.AppendLine("    <version>" + projectConfig.version + "</version>");
            sb.AppendLine("    <packaging>pom</packaging>");
            sb.AppendLine("");
            sb.AppendLine("    <modules>");
            sb.AppendLine("        <module>eurekaserver</module>");
            sb.AppendLine("        <module>configserver</module>");
            sb.AppendLine("        <module>modules</module>");
            sb.AppendLine("    </modules>");

            sb.AppendLine("");
            sb.AppendLine("    <parent>");
            sb.AppendLine("        <groupId>org.springframework.boot</groupId>");
            sb.AppendLine("        <artifactId>spring-boot-starter-parent</artifactId>");
            sb.AppendLine("        <version>1.5.13.RELEASE</version>");
            sb.AppendLine("    </parent>");
            sb.AppendLine("");
            sb.AppendLine("    <properties>");
            sb.AppendLine("        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>");
            sb.AppendLine("        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>");
            sb.AppendLine("        <java.version>1.8</java.version>");
            sb.AppendLine("    </properties>");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("    <dependencyManagement>");
            sb.AppendLine("        <dependencies>");
            sb.AppendLine("            <dependency>");
            sb.AppendLine("                <groupId>org.springframework.cloud</groupId>");
            sb.AppendLine("                <artifactId>spring-cloud-dependencies</artifactId>");
            sb.AppendLine("                <version>Dalston.SR5</version>");
            sb.AppendLine("                <type>pom</type>");
            sb.AppendLine("                <scope>import</scope>");
            sb.AppendLine("            </dependency>");
            sb.AppendLine("        </dependencies>");
            sb.AppendLine("    </dependencyManagement>");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("</project>");

            return sb;
        }
    

    }
}
